package wol;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class WOL {	
	public static void main(String[] args) throws IOException {
		String mac = "50:E5-49-53-A2-A1";
		String ip = "192.168.178.68";
		mp2net(createMP(mac2byte(mac)), ip, 9);
	}
	
	private static boolean mp2net(byte[] mp, String ip, int port) throws IOException {
		InetAddress address = InetAddress.getByName(ip);
        DatagramPacket packet = new DatagramPacket(mp, 102, address, port);
        DatagramSocket socket = new DatagramSocket();
        socket.send(packet);
        socket.close();
		
		return true;
	}
	
	private static byte[] mac2byte(String in) {
        byte[] retValue = new byte[6];
        String[] hex = in.split("(\\:|\\-)");
        for (int i = 0; i < hex.length; i++) {
        	retValue[i] = (byte)Integer.parseInt(hex[i], 16);
        }
        return retValue;
    }
	
	private static byte[] createMP(byte[] mac) {
		byte[] mp = new byte[102];
		for(int i = 0; i < 6; i++) {
			mp[i] = (byte) 0xFF;
		}
		for(int i = 6; i < 102; i++) {
			mp[i] = mac[i%6];
		}
		return mp;
	}
}
